package com.utils.httphelper;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 通过servlet或者filter读取request中的body
 * @author XR-8
 *
 */
public class ReadRequestBody {
/*	读取request中的parameter
 * Enumeration<?> pNames = request.getParameterNames();
	Map<String, String> params = new HashMap<String, String>();
	while (pNames.hasMoreElements()) {
		String pName = (String) pNames.nextElement();
		String pValue = request.getParameter(pName);
		params.put(pName, pValue);
	}*/
	
	  //字符串读取  请求request的body
    private static String charReader(HttpServletRequest request) throws IOException {
        BufferedReader br = request.getReader();
        String str, wholeStr = "";
        while((str = br.readLine()) != null){
           wholeStr += str;
        }
        return wholeStr;
   }

    //二进制读取   请求request的body
	private static String binaryReader(HttpServletRequest request) throws IOException {
		/*
		 * int len = request.getContentLength(); ServletInputStream iii; iii =
		 * request.getInputStream(); byte[] buffer = new byte[len]; int wholeInt =
		 * iii.read(buffer, 0, len); return wholeInt;
		 */

		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedReader = null;
		try {
			InputStream inputStream = request.getInputStream();
			if (inputStream != null) {
				bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
				char[] charBuffer = new char[128];
				int bytesRead = -1;
				while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
					stringBuilder.append(charBuffer, 0, bytesRead);
				}
			} else {
				stringBuilder.append("");
			}
		} catch (IOException ex) {
			throw ex;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException ex) {
					throw ex;
				}
			}
		}
		return stringBuilder.toString();
	}
	
    
    //InputStreamReader 和StringBuilder 本人在使用
    public static String InputStreamToString1(InputStream inputStream) {
    	final int bufferSize = 1024;
    	final char[] buffer = new char[bufferSize];
    	final StringBuilder out = new StringBuilder();
    	Reader in;
		try {
			in = new InputStreamReader(inputStream, "UTF-8");
			for (;;) {
				int rsz = in.read(buffer, 0, buffer.length);
				if (rsz < 0)
					break;
				out.append(buffer, 0, rsz);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return out.toString();
    	
		/*StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedReader = null;
    	if (inputStream != null) {
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			char[] charBuffer = new char[128];
			int bytesRead = -1;
			while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
				stringBuilder.append(charBuffer, 0, bytesRead);
			}
		} else {
			stringBuilder.append("");
		}
    	return stringBuilder.toString();*/
    }
    
    // 使用ByteArrayOutputStream 和 inputStream.read (JDK)
    public static String InputStreamToString2(InputStream inputStream){
    	ByteArrayOutputStream result = new ByteArrayOutputStream();
    	byte[] buffer = new byte[1024];
    	int length;
    	try {
			while ((length = inputStream.read(buffer)) != -1) {
			    result.write(buffer, 0, length);
			}
			result.flush();
			return result.toString("UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
}
